package com.css.fxfzmh.modules.symbolManage.service.impl;

import com.css.fxfzmh.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzmh.base.attachment.service.AttachmentInfoService;
import com.css.fxfzmh.constants.YNEnum;
import com.css.fxfzmh.modules.symbolManage.entity.Symbol;
import com.css.fxfzmh.modules.symbolManage.param.SymbolParam;
import com.css.fxfzmh.modules.symbolManage.repository.SymbolRepository;
import com.css.fxfzmh.modules.symbolManage.service.SymbolService;
import com.css.fxfzmh.util.CreateGson;
import com.css.fxfzmh.util.SvgUtil;
import com.css.fxfzmh.util.UUIDGenerator;
import com.css.fxfzmh.web.PlatformSessionContext;
import com.css.fxfzmh.zcpt.sys.entity.SUser;
import com.css.fxfzmh.zcpt.sys.service.impl.SUserServiceImpl;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/05/08
 * @Description: 图形符号管理Service
 */
@Service
public class SymbolServiceImpl implements SymbolService {

    @Resource
    SymbolRepository symbolRepository;
    @Resource
    SUserServiceImpl sUserServiceImpl;
    @Resource
    private AttachmentInfoService attachmentInfoService;
    @Value("${image.localDir}")
    private String localDir;

    @Override
    public List<Symbol> getPage(SymbolParam param) {
        return symbolRepository.getPage(param);
    }

    @Override
    public void save(Symbol symbol) {
        symbol.setId(UUIDGenerator.getUUID());
        symbol.setCreateUser(PlatformSessionContext.getUserID());
        symbol.setCreateTime(new Date());
        symbol.setDelFlag(YNEnum.N.toString());
        //获取用户名称
        String sUser = sUserServiceImpl.getSUser(PlatformSessionContext.getUserID());
        SUser userInfo = CreateGson.createGson().fromJson(sUser, SUser.class);
        symbol.setUserName(userInfo.getUserName());
        if (symbol.getSvgFlag().equals("1")) {
            AttachmentInfoEntity attach = attachmentInfoService.getAttach(symbol.getAttachId());
            String attachPath = attach.getAttachPath();
            String path = localDir + attachPath;
            symbol.setSvg(SvgUtil.toString(path));
        }
        symbolRepository.save(symbol);
    }

    @Override
    public void delete(String ids) {
        symbolRepository.delete(Arrays.asList(ids.split(",")));
    }
}
